﻿using Newtonsoft.Json;
using SmartPlant.Model.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace SmartPlant.Services
{
    public class HookUpService : BaseService, IHookUpService
    {
        private readonly IItemListService _itemListService;
        public HookUpService(IItemListService itemListService)
        {
            _itemListService = itemListService;
        }

        public async Task<List<HookUpType>> GetHookUpTypeTreeAsync(string proId)
        {
            string url = $"/api/Project/{proId}/hook-up-type-api/tree";
            var res = await GetAsync<List<HookUpType>>(url);
            return res.data;
        }

        public async Task<HookUp> GetHookUpAsync(string proId, string hookUpTypeId)
        {
            string url = $"/api/Project/{proId}/Hook-Up-api/{hookUpTypeId}";
            var res= await GetAsync<HookUp>(url);
            return res.data;
        }

        public async Task<HookUp> GetHookUpDetailAsync(string proId, string hookUpTypeId)
        {
            string url = $"/api/Project/{proId}/Hook-Up-api/detail/{hookUpTypeId}";
            var res = await GetAsync<HookUp>(url);
            return res.data;
        }

        public async Task<dynamic> LoadDynamicHookUpComponents(string jsonStr)
        {
            var propertyInfos = await _itemListService.InitHookUpComponentPropertyInfosAsync(GlobalObject.CurrentProject.ProjId.ToString());
            var objType = DynamicClassHelper.CreateDynamicType(GlobalObject.HookUpComponentEntityName, propertyInfos);
            if (objType == null)
            {
                throw new System.ArgumentNullException(nameof(objType), $"未找到对应的{nameof(objType)}实体信息!!!");
            };
            var listType = typeof(ObservableCollection<>);
            var constructedListType = listType.MakeGenericType(objType);
            var data = JsonConvert.DeserializeObject(jsonStr, constructedListType);
            return data;
        }
    }
}
